<template>
  <div class="form-properties">
    <el-form label-position="top">
      <el-form-item label="表单配置">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-input
              v-model="properties.labelWidth"
              placeholder="标签宽度"
              @change="updateProperties" />
          </el-col>
          <el-col :span="12">
            <el-select
              v-model="properties.labelPosition"
              placeholder="标签位置"
              @change="updateProperties">
              <el-option label="左对齐" value="left" />
              <el-option label="右对齐" value="right" />
              <el-option label="顶部对齐" value="top" />
            </el-select>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="表单项">
        <div v-for="(item, index) in properties.items" :key="index" class="form-item">
          <el-row :gutter="10">
            <el-col :span="6">
              <el-input
                v-model="item.label"
                placeholder="标签"
                @change="updateProperties" />
            </el-col>
            <el-col :span="6">
              <el-input
                v-model="item.field"
                placeholder="字段名"
                @change="updateProperties" />
            </el-col>
            <el-col :span="6">
              <el-select
                v-model="item.type"
                placeholder="类型"
                @change="updateProperties">
                <el-option label="输入框" value="input" />
                <el-option label="下拉框" value="select" />
                <el-option label="日期选择" value="date" />
                <el-option label="单选框" value="radio" />
                <el-option label="多选框" value="checkbox" />
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-checkbox
                v-model="item.required"
                @change="updateProperties">
                必填
              </el-checkbox>
            </el-col>
            <el-col :span="2">
              <el-button type="danger" @click="removeItem(index)">
                <el-icon><Delete /></el-icon>
              </el-button>
            </el-col>
          </el-row>
        </div>

        <el-button type="primary" @click="addItem">
          <el-icon><Plus /></el-icon>
          添加表单项
        </el-button>
      </el-form-item>

      <el-form-item label="按钮配置">
        <el-checkbox
          v-model="properties.showSubmit"
          @change="updateProperties">
          显示提交按钮
        </el-checkbox>
        <el-checkbox
          v-model="properties.showReset"
          @change="updateProperties">
          显示重置按钮
        </el-checkbox>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { Delete, Plus } from '@element-plus/icons-vue'

const props = defineProps({
  value: {
    type: Object,
    default: () => ({
      labelWidth: '100px',
      labelPosition: 'right',
      items: [],
      showSubmit: true,
      showReset: true
    })
  }
})

const emit = defineEmits(['update'])

const properties = ref({ ...props.value })

watch(() => props.value, (newVal) => {
  properties.value = { ...newVal }
}, { deep: true })

const addItem = () => {
  properties.value.items.push({
    label: '',
    field: '',
    type: 'input',
    required: false
  })
  updateProperties()
}

const removeItem = (index) => {
  properties.value.items.splice(index, 1)
  updateProperties()
}

const updateProperties = () => {
  emit('update', { ...properties.value })
}
</script>

<style scoped>
.form-item {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid var(--el-border-color-light);
  border-radius: 4px;
}
</style> 